Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

library(leaflet)
library(sf)
## Linking to GEOS 3.8.1, GDAL 3.2.1, PROJ 7.2.1
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.3     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   2.0.1     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(tidycensus)
library(ggthemes)
library(ggspatial)
library(htmlwidgets)
library(tidytransit)
vars2010 <- load_variables(2010, "sf1")
boston_latinx <- get_decennial(geography = "tract",
                          state = "MA", county = "suffolk",
                          year = 2010,
                          output = "wide",
                          variables = c(tot_pop = 'P004001',
                                        la_pop = 'P004003'),
                          geometry = TRUE)
## Getting data from the 2010 decennial Census
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Using Census Summary File 1
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |======================================================================| 100%
boston_latinx <- boston_latinx %>%
  mutate(pct_latinx = la_pop / tot_pop) %>%
  filter(tot_pop > 0)
acs_vars <- load_variables(2019, "acs5")
v19 <- load_variables(2019, "acs5", cache = TRUE)

##Latinx Population in East Boston (Interactive) - Census Blocks

MA_latinx_palette <- colorNumeric(c("lightpink", "yellow"), boston_latinx$pct_latinx)

latinx_map1 <- leaflet(boston_latinx) %>%
  addProviderTiles("Stamen.TonerLite") %>%
  addPolygons(color = ~MA_latinx_palette(pct_latinx), stroke = FALSE, fillOpacity = 0.7,
              highlightOptions = highlightOptions(fillColor = "orange", fillOpacity = 0.9),
              label = boston_latinx$NAME,
               popup = paste("Total population: ", boston_latinx$tot_pop, "<br/>",
                            "Latinx population: ", boston_latinx$la_pop, " (", 
                            round(boston_latinx$pct_latinx * 100, 1), "%)", sep = "")) %>%
  addLegend("bottomright", pal = MA_latinx_palette, values = ~pct_latinx,
    title = "Percent of population<br/>identifying as Hispanic or Latinx",
    labFormat = labelFormat(suffix = "%",
                            transform = function(x) 100 * x),
    opacity = 1)
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
latinx_map1

##Latinx Population (Static Map)

MA_state_plane <- "+proj=lcc +lat_1=42.68333333333333 +lat_2=41.71666666666667 +lat_0=41 +lon_0=-71.5 +x_0=200000.0001016002 +y_0=750000 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 +no_defs"

ggplot(boston_latinx) +
  annotation_map_tile(zoomin = 0, progress = "none", type = "stamenbw") +
  geom_sf(color = NA, aes(fill = pct_latinx), alpha = 0.7) +
  coord_sf(crs = MA_state_plane) +
  scale_fill_continuous(low="cornsilk", high="darkgreen", 
                       na.value=NA,
                       name = "Percent of population\nidentifying as Latinx",
                       breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1),
                       labels = c("0", "20%", "40%", "60%", "80%", "100%")) +
  theme_void() 
## Loading required namespace: raster

saveWidget(latinx_map1, file="latinx_map_TRACT.html")

boston_latinx <- get_decennial(geography = "block",
                          state = "MA", county = "suffolk",
                          year = 2010,
                          output = "wide",
                          variables = c(tot_pop = 'P004001',
                                        la_pop = 'P004003'),
                          geometry = TRUE)
## Getting data from the 2010 decennial Census
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Using Census Summary File 1
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |=====================================                                 |  54%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
boston_latinx <- boston_latinx %>%
  mutate(pct_latinx = la_pop / tot_pop) %>%
  filter(tot_pop > 0)

##Latinx Population - Census Blocks

MA_latinx_palette <- colorNumeric(c("lightpink", "yellow"), boston_latinx$pct_latinx)

latinx_map_TRACT <- leaflet(boston_latinx) %>%
  addProviderTiles("Stamen.TonerLite") %>%
  addPolygons(color = ~MA_latinx_palette(pct_latinx), stroke = FALSE, fillOpacity = 0.7,
              highlightOptions = highlightOptions(fillColor = "orange", fillOpacity = 0.9),
              label = boston_latinx$NAME,
               popup = paste("Total population: ", boston_latinx$tot_pop, "<br/>",
                            "Latinx population: ", boston_latinx$la_pop, " (", 
                            round(boston_latinx$pct_latinx * 100, 1), "%)", sep = "")) %>%
  addLegend("bottomright", pal = MA_latinx_palette, values = ~pct_latinx,
    title = "Percent of population<br/>identifying as Hispanic or Latinx",
    labFormat = labelFormat(suffix = "%",
                            transform = function(x) 100 * x),
    opacity = 1)
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
latinx_map_TRACT
saveWidget(latinx_map_TRACT, file="latinx_map_BLOCK.html")
medianlatinx_Suffolk <- get_acs(geography = "tract", county = "Suffolk", state = "MA", 
                           year = 2019, survey = "acs5",
                           variables = c(latinx_income = "B19013I_001"),
                           output = "wide", geometry = TRUE) 
## Getting data from the 2015-2019 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
medianlatinx_Suffolk <- medianlatinx_Suffolk %>%
  select(-latinx_incomeM) %>%
  filter(latinx_incomeE > 0)
income_palette <- colorNumeric(c("purple", "lightblue"),
                                medianlatinx_Suffolk$latinx_incomeE)

medianincome_map <- leaflet(medianlatinx_Suffolk) %>%
  addProviderTiles("Stamen.TonerLite") %>%
  addPolygons(fillColor = ~income_palette(latinx_incomeE), weight = 1, color = "gray", fillOpacity = 0.7,
              highlightOptions = highlightOptions(fillColor = "yellow", fillOpacity = 0.9),
              label = medianlatinx_Suffolk$NAME,
              popup = paste("Median Income: ", medianlatinx_Suffolk$latinx_incomeE, "<br/>")) %>%
    addLegend("bottomright", pal = income_palette, values = ~latinx_incomeE,
    title = "Median Household Income<br/>for Latinx Household",
    labFormat = labelFormat(suffix = "$"),
    opacity = 1)
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
medianincome_map
saveWidget(medianincome_map, file="medianincome_map_TRACT.html")
latinx_pop_Suffolk <- get_acs(geography = "tract", county = "Suffolk", state = "MA", 
                           year = 2019, survey = "acs5",
                           variables = c(tot_pop = "B01003_001", latinx_pop = "B03001_001"),
                           output = "wide", geometry = TRUE) 
## Getting data from the 2015-2019 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
latinx_pop_Suffolk <- latinx_pop_Suffolk %>%
  filter(tot_popE > 0) %>%
  mutate(pct_latinx = round(100 * latinx_popE / tot_popE, 1))
latinx_pop_palette <- colorNumeric(c("lightpink", "yellow"),
                                latinx_pop_Suffolk$pct_latinx)

latinx_pop_map <- leaflet(latinx_pop_Suffolk) %>%
  addProviderTiles("Stamen.TonerLite") %>%
  addPolygons(fillColor = ~latinx_pop_palette(pct_latinx), weight = 1, color = "gray", fillOpacity = 0.7,
              highlightOptions = highlightOptions(fillColor = "yellow", fillOpacity = 0.9),
              label = latinx_pop_Suffolk$NAME,
              popup = paste("Total workers: ", latinx_pop_Suffolk$tot_popE, "<br/>",
                            "Transit commuters: ", latinx_pop_Suffolk$latinx_popE, " (", 
                            latinx_pop_Suffolk$pct_latinx, "%)", sep = "")) %>%
    addLegend("bottomright", pal = latinx_pop_palette, values = ~pct_latinx,
    title = "Percent of population<br/>identifying as Hispanic/Latinx",
    labFormat = labelFormat(suffix = "%"),
    opacity = 1)
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
latinx_pop_map